<?php


namespace app\zhongmaotong\controller;




use app\zhongmaotong\model\BannerModel;
use app\zhongmaotong\validate\BannerValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class BannerController extends AdminBaseController
{
    public function index()
    {
        // 查询所有新闻并传递给视图
        $newsList = BannerModel::
        order("list_order", 'desc')
            ->paginate(5); // 每页显示2条记录
        $page = $newsList->render();
        $this->assign('newsList', $newsList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch('add');
    }
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = input('post.');
//                halt($data['image']);
            //验证数据
            $validate = new BannerValidate();
            $result = $validate->scene('add')->check($data);
            if (!$result) {
                return json(['code' => 0, 'msg' => $validate->getError(), 'data' => null]);
            }
            $result = BannerModel::create($data);
            if ($result) {
                $this->success('添加成功', url('Banner/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }


    public function show($id)
    {
        $news = BannerModel::find($id);
        if($news['switch'] == 1){
            $news->switch = 0;
            if ($news->save()) {
                $this->success('编辑成功', url('Banner/index'));
            } else {
                $this->error('编辑失败');
            }
        }else{
            $news->switch = 1;
            if ($news->save()) {
                $this->success('编辑成功', url('Banner/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }
    public function edit($id)
    {
        $news = BannerModel::find($id);
        if (!$news) {
            $this->error('图片不存在');
        }
        $this->assign('news', $news);

        return $this->fetch('edit');
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = input('post.');

            $news = BannerModel::find($data['id']);
            if (!$news) {
                $this->error('新闻不存在');
            }
            //验证数据
            $validate = new BannerValidate();
            $result = $validate->scene('edit')->check($data);
            if (!$result) {
                return json(['code' => 0, 'msg' => $validate->getError(), 'data' => null]);
            }

            //switch的默认值
            $data['switch'] = 1;

            $news->url = $data['url'];
            $news->image = $data['image'];
            $news->switch = $data['switch'];
            $news->update_time = date('Y-m-d H:i:s');
            if ($news->save()) {
                $this->success('编辑成功', url('Banner/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }


    public function delete($id)
    {
        $news = BannerModel::find($id);
        if (!$news) {
            $this->error('图片不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new BannerModel());
        $this->success('排序成功');
    }
}